import * as types from "./mutation-type";
// import { playMode } from "@/common/playConfig";
// import utils from "@/utils/utils";
import {
  // saveSearch,
  // deleteSearch,
  // clearSearch,
  // saveHistory,
  // deleteHistory,
  // clearHistory,
  addPlayList,
  delPlayList,
} from "@/common/cache";

function findIndex(list, songIndex) {
  return list.findIndex((item) => {
    return item.trackId === songIndex;
  });
}

// 选择播放
export const selectPlay = function ({ commit }, { list, index }) {
  index = findIndex(list, index);
  commit(types.SET_PLAYLIST, list);
  commit(types.SET_CURRENT_INDEX, index);
  commit(types.SET_PLAYING_STATE, true);
};

// 播放全部
export const playAll = function ({ commit }, { list }) {
  // commit(types.SET_PLAY_MODE, playMode.sequence);
  // commit(types.SET_SEQUENCE_LIST, list);
  commit(types.SET_PLAYLIST, list);
  commit(types.SET_CURRENT_INDEX, 0);
  commit(types.SET_PLAYING_STATE, true);
};

// 暂停播放
export const playStatus = function ({ commit }, flag) {
  commit(types.SET_PLAYING_STATE, flag);
};

// 移除播放
export const stopPlay = function ({ commit }) {
  commit(types.SET_PLAYING_STATE, false);
  commit(types.SET_PLAYLIST, []);
  commit(types.SET_SEQUENCE_LIST, []);
  commit(types.SET_CURRENT_INDEX, -1);
  commit(types.SET_PLAY_PARENT_ID, null);
  commit(types.SET_PLAY_CLIP_ID, null);
};

// 保存搜索历史
// export const saveSearchHistory = function ({ commit }, query) {
//   commit(types.SET_SEARCH_HISTORY, saveSearch(query));
// };

// 移除单个搜索历史
// export const deleteSearchHistory = function ({ commit }, query) {
//   commit(types.SET_SEARCH_HISTORY, deleteSearch(query));
// };

// 移除全部搜索历史
// export const clearSearchHistory = function ({ commit }) {
//   commit(types.SET_SEARCH_HISTORY, clearSearch());
// };

// 保存播放列表
export const savePlayList = function ({ commit }, { list, location }) {
  commit(types.SAVE_PLAYLIST, addPlayList(list, location));
};

// 移除播放列表
export const clearPlayList = function ({ commit }, list) {
  commit(types.SAVE_PLAYLIST, delPlayList(list));
};

// 设置最近播放歌曲
// export const saveHistoryList = function ({ commit }, song) {
//   commit(types.SET_HISTORY_LIST, saveHistory(song));
// };

// 移除最近播放歌曲单个
// export const deleteHistoryList = function ({ commit }, song) {
//   commit(types.SET_HISTORY_LIST, deleteHistory(song));
// };

// 移除全部最近播放
// export const clearHistoryList = function ({ commit }) {
//   commit(types.SET_HISTORY_LIST, clearHistory());
// };
